﻿using System;
using System.Drawing;
using System.Linq;
using System.Web.Security;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class AwaitingUsers : BaseUserControl
    {
        private static Color RedColor = Color.FromArgb(
                        Convert.ToInt32("44", 16),
                        Convert.ToInt32("00", 16),
                        Convert.ToInt32("00", 16));

        protected void Page_Load(object sender, EventArgs e)
        {
            var query = (from awu in ForumInfo.AwaitingUsers
                         orderby awu.UserName
                         select new
                         {
                             awu.ProviderUserKey,
                             awu.UserName,
                             awu.Email,
                             awu.CreationDate,
                             awu.LastLoginDate,
                             awu.LastActivityDate,
                             awu.LastLockoutDate,
                             awu.LastPasswordChangedDate,
                             awu.IsApproved,
                             awu.IsLockedOut,
                             NbRoleOwned = Roles.GetRolesForUser(awu.UserName).Count()
                         });
            AwaitingUserGridView.DataSource = query;
            AwaitingUserGridView.DataBind();
        }


        protected void AwaitingUserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {                
                var IsApprovedCell = e.Row.Cells[8];
                var IsApproved = e.Row.Cells[8].Controls[0] as CheckBox;
                if (IsApproved != null && !IsApproved.Checked)
                    IsApprovedCell.BackColor = RedColor;

                var IsLockedOutCell = e.Row.Cells[9];
                var IsLockedOut = e.Row.Cells[9].Controls[0] as CheckBox;
                if (IsLockedOut != null && IsLockedOut.Checked)
                    IsLockedOutCell.BackColor = RedColor;

                var NbRoleOwnedCell = e.Row.Cells[10];

                var NbRoleOwned = Int32.Parse(e.Row.Cells[10].Text);
                if (NbRoleOwned == 0)
                    NbRoleOwnedCell.BackColor = RedColor;
            }
        }

        protected void AwaitingUserGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string command = e.CommandName;
            var argument = e.CommandArgument;
        }
    }

}
